import { useLocation } from "umi"
import { getBookByIdApi,editBookInfoApi } from "@/services/book";
import { useState, useEffect } from "react";
import { PageContainer } from '@ant-design/pro-components';
import {useNavigate} from "umi"
import {message} from "antd"
// import { getBooksApi } from "../../services/book";
import BookForm from "./components/bookForm"
export default function EditBook() {
    const navigator = useNavigate()
    const [useInfo, setUserInfo] = useState({})
    const location = useLocation()
    console.log(location)
    // 拿到当前的书籍id
    const id = location.state.id
    useEffect(() => {
        // 请求数据
        console.log(id, "看这里")
        getBookByIdApi(id).then(res => {
            // console.log(res,"书籍信息")
            setUserInfo(res.data)
            
        })

    }, [])
    function submit(tar){
        console.log(tar);
        console.log(useInfo,"书籍信息")
        editBookInfoApi(useInfo._id,tar).then(res=>{
            // 修改成功
            navigator('/book/bookList');
            message.success('编辑成功')
        })
    }
    return (
        <PageContainer header={{
            breadcrumb: {
              items: [
                {
                  title: '书籍',
                },
                {
                  title: '编辑书籍',
                },
              ],
            },
          }} >

            <BookForm type='edit' submit={submit} initData={useInfo} />
        </PageContainer>

    )
}